package yx.action;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;

import yx.service.HobbyServiceInt;
import yx.vo.DatagridVO;
import yx.vo.HobbyVO;
import yx.vo.JsonVO;

import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
@Namespace("/")
@Action(value = "hobbyAction")
public class HobbyAction extends BaseAction implements ModelDriven<HobbyVO> {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(HobbyAction.class);

	private HobbyVO hobby = new HobbyVO();

	@Override
	public HobbyVO getModel() {
		return hobby;
	}

	private HobbyServiceInt hobbyService;

	public HobbyServiceInt getHobbyService() {
		return hobbyService;
	}

	@Autowired
	public void setHobbyService(HobbyServiceInt hobbyService) {
		this.hobbyService = hobbyService;
	}

	public void queryHobby() {
		if (hobby.getStudentId() != null) {
			//super.writeJson(hobbyService.datagrid(hobby));
			DatagridVO datagridVO = hobbyService.datagrid(hobby);
			if(datagridVO.getTotal().intValue() != 0){
				HobbyVO u = (HobbyVO)datagridVO.getRows().get(0);
				super.writeJson(u);
			}
		}
	}

	public void addHobby() {
		JsonVO j = new JsonVO();
		try {
			HobbyVO u = hobbyService.save(hobby);
			j.setSuccess(true);
			j.setMsg("增加成功！");
			j.setObj(u);
		} catch (Exception e) {
			j.setMsg(e.getMessage());
		}
		super.writeJson(j);
	}

	public void removeHobby() {
		hobbyService.remove(hobby.getIds());
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}

	public void editHobby() {
		HobbyVO u = hobbyService.edit(hobby);
		JsonVO j = new JsonVO();
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		j.setObj(u);
		super.writeJson(j);
	}

	public void addOrEditHobby() {
		if (hobby.getId() == null) {
			JsonVO j = new JsonVO();
			try {
				HobbyVO u = hobbyService.save(hobby);
				j.setSuccess(true);
				j.setMsg("增加成功！");
				j.setObj(u);
			} catch (Exception e) {
				j.setMsg(e.getMessage());
			}
			super.writeJson(j);
		} else {
			HobbyVO u = hobbyService.edit(hobby);
			JsonVO j = new JsonVO();
			j.setSuccess(true);
			j.setMsg("编辑成功！");
			j.setObj(u);
			super.writeJson(j);
		}
	}
}
